<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { createTableDataApi, deleteTableDataApi, getTableDataApi, batDeleteTableDataApi } from "@/api/buy-ticket"
import { type CreateTableRequestData, type TableData } from "@/api/buy-ticket/types/table"
import { type FormInstance, type FormRules, ElMessage, ElMessageBox, Table } from "element-plus"
import { Search, Refresh, CirclePlus, Delete, Download, RefreshRight } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
import { cloneDeep } from "lodash-es"
import { da, ro } from "element-plus/es/locales.mjs"

defineOptions({
  // 命名当前组件
  name: "ElementPlus"
})

const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()

//#region 增
const DEFAULT_FORM_DATA: CreateTableRequestData = {
  checiId: undefined,
  startPlace: "",
  endPlace: "",
  boardingTime: "",
  dropOffTime: "",
  price: undefined
}
const dialogVisible = ref<boolean>(false)
const formRef = ref<FormInstance | null>(null)
const tableRef = ref()
const formData = ref<CreateTableRequestData>(cloneDeep(DEFAULT_FORM_DATA))
// 处理点击购买按钮
const handleBuy = (row: TableData) => {
  ElMessageBox.confirm(`确认购买车票(车次：${row.checiId}):`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    loading.value = true
    const api = createTableDataApi
    formData.value.checiId = row.checiId
    formData.value.boardingTime = row.boardingTime
    formData.value.dropOffTime = row.dropOffTime
    formData.value.startPlace = searchData.startPlace
    formData.value.endPlace = searchData.endPlace
    formData.value.price = row.price
    api(formData.value)
      .then(() => {
        ElMessage.success("购买成功")
        dialogVisible.value = false
      })
      .finally(() => {
        loading.value = false
      })
  })
}
const resetForm = () => {
  formRef.value?.clearValidate()
  formData.value = cloneDeep(DEFAULT_FORM_DATA)
}
//#endregion

//#region 查
const tableData = ref<TableData[]>([])
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  startPlace: "",
  endPlace: ""
})
const getTableData = () => {
  loading.value = true
  getTableDataApi({
    currentPage: paginationData.currentPage,
    size: paginationData.pageSize,
    startPlace: searchData.startPlace || undefined,
    endPlace: searchData.endPlace || undefined
  })
    .then(({ data }) => {
      paginationData.total = data.total
      tableData.value = data.list
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}

// 点击查询时调用
const handleSearch = () => {
  paginationData.currentPage === 1 ? getTableData() : (paginationData.currentPage = 1)
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  handleSearch()
}
//#endregion

/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <div class="app-container">
  <!-- <div>这是购票页面</div> -->

    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="startPlace" label="出发地">
          <el-input v-model="searchData.startPlace" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="endPlace" label="目的地">
          <el-input v-model="searchData.endPlace" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-tooltip content="刷新当前页">
            <el-button type="primary" :icon="RefreshRight" circle @click="getTableData" />
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData" ref="tableRef">
          <!-- <el-table-column type="selection" width="50" align="center" /> -->
          <el-table-column prop="checiId" label="车次编号" align="center" />
          <el-table-column prop="type" label="车型" align="center" />
          <el-table-column prop="vehicleId" label="车牌号" align="center" />
          <el-table-column prop="boardingTime" label="上车时间" align="center" />
          <el-table-column prop="dropOffTime" label="下车时间" align="center" />
          <el-table-column prop="seats" label="余票" align="center" />
          <el-table-column prop="price" label="价格" align="center" />
          <el-table-column fixed="right" label="操作" width="150" align="center">
            <template #default="scope">
              <el-button type="success" text bg size="small" @click="handleBuy(scope.row)">购买</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
